package tablization;


import java.util.Hashtable;
import java.util.Vector;
import dbmanager.DBManager;

/**
 *  @author Shweta
 **/
public class TableInsertion extends SubTable 
{
	
	public String []tableArray;
	 
	public TableInsertion(DBManager sqlDB) 
	{
		super.setDBManager(sqlDB);
	}	
	
	
	
	
	/**
	 * this function inserts properties in table
	 * @param propertyVector - vector of properties to be inserted in tables
	 * @return vector containing remaining properties
	 */
	@SuppressWarnings("unchecked")
	public Vector<Object> getInsert(Vector<Object> propertyVector)
	{
	
		int k =0;
		String tableName,strMid;
		Hashtable<Object,Object> insertHash;		
		Vector<Object> insertVector=new Vector<Object>();
		Vector<Object> vector = propertyVector;
		
		//String []tableArray = subArray;//check
			strMid = ((Hashtable<Object, Object>)vector.get(k)).get("mid").toString();
			
			for(int i=0;i<tableArray.length;i++)
			{
				
				tableName = tableArray[i].split("\t")[0];
				String strTableName = gin.getItem(tableName);

				//retrieve columns of given table
				Hashtable<Object,Object> arrayHash = new Hashtable<Object, Object>();				
				
				arrayHash.put(PID_NAME, "column");
				arrayHash.put(keyName, "mid");
				arrayHash.put(valueName, tableName);
				
				String [] columnArray= getArray(arrayHash);
				 
				insertHash = new Hashtable<Object, Object>();
				insertHash.put("id",strMid);
				 
				//checks whether table is unique/multiple valued
				 if(checkTableCondition(tableName)==true) 
				 {
					 for(int j=0;j<vector.size();j++ )
					 {
						 Hashtable<Object,Object> hasht = (Hashtable<Object, Object>)vector.get(j);
						
						 for(int x=0;x<columnArray.length;x++)
						 {
							 String column = columnArray[x].split("\t")[2]; 
							 if(hasht.get("pid").equals(column))
							 {
								insertHash.put(strTableName+"."+ gin.getItem(column).toLowerCase(), getColumnValue(hasht)) ;
								 
								vector.remove(j);	
								try	{
									j--;
								} catch (Exception e) {
									j=0;
								}
								sqlDB.getInsert(strTableName, insertHash);
								break;
							 }
						 }						
					 }
					 System.out.println("Inserted in multiple-valued Table");
				 }
				 else
				 {	 
					 for(int j=0;j<columnArray.length;j++)
					 {
						 String column = columnArray[j].split("\t")[2];
						 k=0;
						 while(k<vector.size())
						 {
							 Hashtable<Object,Object> hasht = (Hashtable<Object, Object>)vector.get(k);
							
							 if(hasht.get("pid").equals(column))
							 {										 
								insertHash.put(strTableName+"."+ gin.getItem(column).toLowerCase(), getColumnValue(hasht)) ;
								
								vector.remove(k);
								break;
							 }
							 k++;
						 }					
					 }				 
					 sqlDB.getInsert(strTableName, insertHash);
				
					 System.out.println("Inserted in unique-valued Table");
				 }				
				insertVector.add(getMasterHashtable(gin.getItem(strMid),"tablization",strTableName));					
			}
			
			//to insert in pl
			callInsertPropertyBean(insertVector);
		
			 System.out.println("Insertion in Table completed");
					 
		return vector;
	}
	
	
	/**
	 * this function sets the tablearray
	 * @param tablearray - string array of table 
	 */
	public void setTableArray(String [] tablearray)
	{
		this.tableArray = tablearray; 
	}
	

	
	public static void main(String[] args)
	{
		
		//String request="11 master_table add *obj44 |Type:objType |city:Pune |employee :osc |add:MH |mobileno :98666";
	//	11 master_table add*Deepak6 |Type :employee |city :Raipur |add :CG |mobileno :111 |mobileno :222 |qualification :BCA |qualification :MCA  #605
		 
		
		DBManager db=new DBManager();
		db.getConnect();
		
		TableInsertion ti=new TableInsertion(db);	
		
		Vector<Object> vector = new Vector<Object>();
		Hashtable<Object,Object> hasht = new Hashtable<Object, Object>();
//		|Type :Human2 |product :Lux |product :ThumsUp |product :Parle |color :white |color :black |color :red |reference :ASC |reference :CSC |reference :DCA
		hasht.put("mid","aaa501");
		hasht.put("pid","Type");
		hasht.put("pv","Human2");
		vector.add(ti.convertInMasterId(hasht));
		
		hasht = new Hashtable<Object, Object>();
		hasht.put("mid","aaa501");
		hasht.put("pid","product");
		hasht.put("pv","Lux");
		vector.add(ti.convertInMasterId(hasht));
			
		
		hasht = new Hashtable<Object, Object>();
		hasht.put("mid","aaa501");
		hasht.put("pid","product");
		hasht.put("pv","ThumsUp");
		vector.add(ti.convertInMasterId(hasht));
		
		hasht = new Hashtable<Object, Object>();
		hasht.put("mid","aaa501");
		hasht.put("pid","product");
		hasht.put("pv","Parle");
		vector.add(ti.convertInMasterId(hasht));
		
		hasht = new Hashtable<Object, Object>();
		hasht.put("mid","aaa501");
		hasht.put("pid","color");
		hasht.put("pv","white");
		vector.add(ti.convertInMasterId(hasht));
				
		hasht = new Hashtable<Object, Object>();
		hasht.put("mid","aaa501");
		hasht.put("pid","color");
		hasht.put("pv","black");
		vector.add(ti.convertInMasterId(hasht));
		
		hasht = new Hashtable<Object, Object>();
		hasht.put("mid","aaa501");
		hasht.put("pid","color");
		hasht.put("pv","red");
		vector.add(ti.convertInMasterId(hasht));
		
		hasht = new Hashtable<Object, Object>();
		hasht.put("mid","aaa501");
		hasht.put("pid","reference");
		hasht.put("pv","ASC");
		vector.add(ti.convertInMasterId(hasht));
		
		hasht = new Hashtable<Object, Object>();
		hasht.put("mid","aaa501");
		hasht.put("pid","reference");
		hasht.put("pv","CSC");
		vector.add(ti.convertInMasterId(hasht));
		
		hasht = new Hashtable<Object, Object>();
		hasht.put("mid","aaa501");
		hasht.put("pid","reference");
		hasht.put("pv","DCA");
		vector.add(ti.convertInMasterId(hasht));
		
		String type = "human2";
		
		if(ti.checkTableExistence(type) == true)
		{
			ti.setTableArray(ti.subArray);
			Vector<Object> v = new Vector<Object>();
			v=ti.getInsert(vector);
			if(!v.isEmpty())
				//call getInsert() of pl having remaining properties in pl
				System.out.println("properties inserted");
			
		}
	}
	
	
	
	
}
